package com.gt.xmall.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;



public class ApplicationUtil {
	
	static Map<String,ApplicationContext> ctxs = new HashMap<String,ApplicationContext>();

	/**
	 * added by Aidy.Q
	 * @param context
	 * @param name
	 * @return
	 */
	public  static Object getSpringBeanByName(ServletContext context,String name){
		
		Object o;
		ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
		o=ctx.getBean(name);
		return o;
		
	}
	
	public  static Object getSpringBeanByName(String xml,String name){
		ApplicationContext ctx =(ApplicationContext) ctxs.get(xml);
		if(ctx ==null){
			ctx = new ClassPathXmlApplicationContext(xml); 
			ctxs.put(xml, ctx);
		}
		Object o;
		 
		o=ctx.getBean(name);
		return o;
		
	}
}
